//https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/

typedef struct stack
{
    int* stk;
    int size;
    int capacity;
}Stack;

Stack* stackcreat(int capacity_)
{
    Stack* new = (Stack*)malloc(sizeof(Stack));
    if (new == NULL)
    {
        perror("malloc fail\n");
        return NULL;
    }
    new->stk = (int*)malloc(sizeof(int) * capacity_);
    new->size = 0;
    new->capacity = capacity_;
    return new;
}

void stackpush(Stack* obj, int val)
{
    obj->stk[obj->size++] = val;
}

void stackpop(Stack* obj)
{
    obj->size--;
}

int stacktop(Stack* obj)
{
    return obj->stk[obj->size - 1];
}

bool stackempty(Stack* obj)
{
    return obj->size == 0;
}

void stackfree(Stack* obj)
{
    free(obj->stk);
    obj->size = 0;
    obj->capacity = 0;
}

typedef struct {
    Stack* in;
    Stack* out;
} CQueue;


CQueue* cQueueCreate() {
    CQueue* ret = (CQueue*)malloc(sizeof(CQueue));
    if (ret == NULL)
    {
        perror("malloc fail\n");
        return NULL;
    }
    ret->in = stackcreat(10000);
    ret->out = stackcreat(10000);
    return ret;

}

void cQueueAppendTail(CQueue* obj, int value) {
    stackpush(obj->in, value);
}

int cQueueDeleteHead(CQueue* obj) {
    if (stackempty(obj->out))
    {
        if (stackempty(obj->in))
        {
            return -1;
        }
        //往要借出的书车导入归还的书
        while (!stackempty(obj->in))
        {
            stackpush(obj->out, stacktop(obj->in));
            stackpop(obj->in);
        }
    }
    //返回借出的书的编号
    int num = stacktop(obj->out);
    stackpop(obj->out);
    return num;

}

void cQueueFree(CQueue* obj) {
    stackfree(obj->in);
    stackfree(obj->out);
}